package com.xinghai.system.service.impl;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.xinghai.common.utils.DateUtils;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.system.mapper.QdDealInfoMapper;
import com.xinghai.system.domain.QdDealInfo;
import com.xinghai.system.service.IQdDealInfoService;

/**
 * 用户交易明细Service业务层处理
 *
 * @author xinghai
 * @date 2024-12-07
 */
@Service
public class QdDealInfoServiceImpl extends ServiceImpl<QdDealInfoMapper, QdDealInfo>
        implements IQdDealInfoService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询用户交易明细列表
     *
     * @param qdDealInfo 用户交易明细
     * @return 用户交易明细集合
     */
    @Override
    public List<QdDealInfo> list(QdDealInfo qdDealInfo) {
        QueryWrapper<QdDealInfo> queryWrapper = new QueryWrapper<>();
        if (qdDealInfo.getStatus()!= null  && !qdDealInfo.getStatus().isEmpty()){
            queryWrapper.eq("status", qdDealInfo.getStatus());
        }
        if (qdDealInfo.getParams().get("beginCreateTime")!= null && !"".equals(qdDealInfo.getParams().get("beginCreateTime"))
        &&qdDealInfo.getParams().get("endCreateTime")!= null && !"".equals(qdDealInfo.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", qdDealInfo.getParams().get("beginCreateTime"),qdDealInfo.getParams().get("endCreateTime"));
        }
        if (qdDealInfo.getPayBankInfo()!= null  && !qdDealInfo.getPayBankInfo().isEmpty()){
            queryWrapper.like("pay_bank_info", qdDealInfo.getPayBankInfo());
        }
        if (qdDealInfo.getPayBankNo()!= null  && !qdDealInfo.getPayBankNo().isEmpty()){
            queryWrapper.like("pay_bank_no", qdDealInfo.getPayBankNo());
        }
        if (qdDealInfo.getPayBankName()!= null  && !qdDealInfo.getPayBankName().isEmpty()){
            queryWrapper.like("pay_bank_name", qdDealInfo.getPayBankName());
        }
        if (qdDealInfo.getPayUserId()!= null ){
            queryWrapper.eq("pay_user_id", qdDealInfo.getPayUserId());
        }
        if (qdDealInfo.getPayUserName()!= null  && !qdDealInfo.getPayUserName().isEmpty()){
            queryWrapper.like("pay_user_name", qdDealInfo.getPayUserName());
        }
        if (qdDealInfo.getPayUserPhone()!= null  && !qdDealInfo.getPayUserPhone().isEmpty()){
            queryWrapper.like("pay_user_phone", qdDealInfo.getPayUserPhone());
        }
        if (qdDealInfo.getUserId()!= null ){
            queryWrapper.eq("user_id", qdDealInfo.getUserId());
        }
        if (qdDealInfo.getUserName()!= null  && !qdDealInfo.getUserName().isEmpty()){
            queryWrapper.like("user_name", qdDealInfo.getUserName());
        }
        if (qdDealInfo.getUserPhone()!= null  && !qdDealInfo.getUserPhone().isEmpty()){
            queryWrapper.like("user_phone", qdDealInfo.getUserPhone());
        }
        if (qdDealInfo.getAmount()!= null ){
            queryWrapper.eq("amount", qdDealInfo.getAmount());
        }
        if (qdDealInfo.getOrderInfoId()!= null ){
            queryWrapper.eq("order_info_id", qdDealInfo.getOrderInfoId());
        }
        if (qdDealInfo.getStoreId()!= null ){
            queryWrapper.eq("store_id", qdDealInfo.getStoreId());
        }
        return this.list(queryWrapper);
    }

    /**
     * 删除用户交易明细信息
     * @param id 需要删除的数据ID
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    public boolean delete(Long id, boolean flag) {
        if (flag){
            QdDealInfo qdDealInfo = new QdDealInfo();
            qdDealInfo.setId(id);
            qdDealInfo.setDelFlag(GenConstants.DELETE_FLAG_YES);
            return this.updateById(qdDealInfo);
        }
        return this.removeById(id);
    }

    /**
     * 批量删除用户交易明细信息
     *
     * @param ids 需要删除的数据ID集合
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids, boolean flag) {
        if (flag){
            List<QdDealInfo> collect = Arrays.stream(ids).map(m -> {
                QdDealInfo qdDealInfo = new QdDealInfo();
                qdDealInfo.setId(m);
                qdDealInfo.setDelFlag(GenConstants.DELETE_FLAG_YES);
                return qdDealInfo;
            }).collect(Collectors.toList());
            return this.updateBatchById(collect);
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 导入用户交易明细信息
     *
     * @param list 用户交易明细信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<QdDealInfo> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            QdDealInfoMapper qdDealInfoMapper = sqlSession.getMapper(QdDealInfoMapper.class);
            list.forEach(qdDealInfoMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
